﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace XhPaper
{
    public interface GetPointInterface
    {
        double[] GetLeftBottomPoint(double[] d);
        double[] GetLeftTopPoint(double[] d);
        double[] GetRightTopPoint(double[] d);
        double[] GetRightBottomPoint(double[] d);
    }
    public static class MyCommonMethod
    {
        /// <summary>
        /// 把有数字组成的字符串，转换为double 数组
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static double[] GetDoubles(string s)
        {
            char[] ch = new char[] { ',' };
            double[] d;
            string[] str = s.Split(ch, StringSplitOptions.RemoveEmptyEntries);
            d = new double[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                double.TryParse(str[i], out d[i]);
            }
            return d;
        }
    }
    public class XhGraphicLine
    {
        public double[] startP;
        public double[] endP;
        public bool m_isBold;
        public XhGraphicLine()
        {
            startP = new double[3];
            endP = new double[3];
        }
        public XhGraphicLine(double[] start, double[] end)
        {
            if (start[0] < end[0])
            {
                startP = start;
                endP = end;
            }
            else
            {
                if (start[0] > end[0])
                {
                    startP = end;
                    endP = startP;
                }
                else
                {
                    if (start[1] < end[1])
                    {
                        startP = start;
                        endP = end;
                    }
                    else
                    {
                        startP = end;
                        endP = startP;
                    }
                }
            }
        }
    }
    public class CadText
    {
        public string m_text;
        public double[] m_coordinate;
        public CadText() 
       {
           m_coordinate = new double[3];
       }
    }
    public class StructClass
    {
        public Dictionary<int, XhGraphicLine> yLineDic = new Dictionary<int, XhGraphicLine>();
        public Dictionary<int, XhGraphicLine> xLineDic = new Dictionary<int, XhGraphicLine>();
        /// <summary>
        /// 与x轴平行的直线列表
        /// </summary>
        public List<XhGraphicLine> m_xLineList = new List<XhGraphicLine>();
        /// <summary>
        /// 与y轴平行的直线列表
        /// </summary>
        public List<XhGraphicLine> m_yLineList = new List<XhGraphicLine>();
        /// <summary>
        /// 既不与x轴也不与y轴平行的直线列表
        /// </summary>
        public List<XhGraphicLine> m_xyLineList = new List<XhGraphicLine>();
        public void AddxLine(int index, double[] p1, double[] p2)
        {
            //xLineDic[index] = new XhGraphicLine(p1, p2);
            m_xLineList.Add(new XhGraphicLine(p1, p2));
            m_xLineList.Sort(compartex);

        }
        public void AddyLine(int index, double[] p1, double[] p2)
        {
           // yLineDic[index] = new XhGraphicLine(p1, p2);
            m_yLineList.Add(new XhGraphicLine(p1, p2));
            m_yLineList.Sort(compartey);
        }
        private int compartey(XhGraphicLine l1, XhGraphicLine l2)
        {
            if (l1.endP[0] > l2.endP[0])
            {
                return 1;
            }
            else
            {
                if (l1.endP[0] < l2.endP[0])
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
        }
        private int compartex(XhGraphicLine l1, XhGraphicLine l2)
        {
            if (l1.endP[1] > l2.endP[1])
            {
                return 1;
            }
            else
            {
                if (l1.endP[1] < l2.endP[1])
                {
                    return -1;
                }
                else
                {
                    return 0;
                }
            }
        }
        /// <summary>
        /// 通过给点的两个点，生成直线存入列表。并进行排序。
        /// </summary>
        /// <param name="p1"></param>
        /// <param name="p2"></param>
        public void AddLine(double[] p1, double[] p2,bool isbold=false)
        {
            XhGraphicLine line = new XhGraphicLine(p1, p2);
            line.m_isBold = isbold;
            if (p1[0] == p2[0])
            {
                m_yLineList.Add(line);
                m_yLineList.Sort(compartey);
            }
            else
            {
                if (p1[1] == p2[1])
                {
                    m_xLineList.Add(line);
                    m_xLineList.Sort(compartex);
                }
                else
                {
                    m_xyLineList.Add(line);
                }
            }

        }
    }
    public class MyObjectBase
    {
        /// <summary>
        /// 相对于父窗体左上角的位置
        /// </summary>
        public PointF m_position;
        /// <summary>
        /// 相对于屏幕左上角的位置
        /// </summary>
        public PointF m_leftTop; 
        /// <summary>
        /// 父窗体类
        /// </summary>
        public MyObjectBase m_parent;
        /// <summary>
        /// 子类列表
        /// </summary>
        public List<MyObjectBase> m_childList=new List<MyObjectBase>();
        public void AddChile(MyObjectBase child)
        {
            child.m_parent = this;
            m_childList.Add(child);
            child.m_leftTop.X = m_position.X+child.m_position.X;
            child.m_leftTop.Y  = m_position.Y + child.m_position.Y;
        }
        public virtual void SetMyObjectFromXML()
        {
        }
    }
}
